package com.springboot42.controller;

import com.springboot42.pojo.City;
import com.springboot42.pojo.Mayor;
import com.springboot42.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CityMayorController {
    @Autowired
    CityService cityService;

    //显示市长——城市信息
    @RequestMapping("mayorlist")
    public String getAllCity(Model model, String name){
        if(name!=null ){
            model.addAttribute("list",cityService.searchMayorByName(name));
        }else{
            model.addAttribute("list",cityService.findAllMayor());
        }
        return "mayorlist.html";
    }

    //添加新市长——城市信息
    @RequestMapping("addmayor")
    public String addmayor(){
        return "addmayor.html";
    }

    //添加新市长——城市信息确认请求
    @RequestMapping("mayorcommit")
    public String personcommit(String mname, String mgender, int mage, String mtel,int carea,String cname,int cpopulation){
        Mayor mayor=new Mayor();
        mayor.setMage(mage);
        mayor.setMname(mname);
        mayor.setMgender(mgender);
        mayor.setMtel(mtel);
        City city=new City();
        city.setCarea(carea);
        city.setCpopulation(cpopulation);
        city.setCname(cname);
        mayor.setCity(city);
        cityService.addMayor(mayor);
        return "redirect:/mayorlist";
    }

    //删除市长及对应城市信息
    @RequestMapping("deleteMayorById")
    public String deleteMayorById(int cid){
        //1对1删除是多次对单表的删除
        cityService.deleteMayorById(cid);

        return "redirect:/mayorlist";
    }

    //发起级联更新市长——城市信息请求
    @RequestMapping("updateMayorCity")
    public String updateMayorCity(Model model,int cid,String mname, String mgender, int mage, String mtel,int carea,String cname,int cpopulation){
        model.addAttribute("cid",cid);
        model.addAttribute("mname",mname);
        model.addAttribute("mgender",mgender);
        model.addAttribute("mage",mage);
        model.addAttribute("mtel",mtel);
        model.addAttribute("carea",carea);
        model.addAttribute("cname",cname);
        model.addAttribute("cpopulation",cpopulation);
        return "updatemayorcity.html";
    }

    //确认级联更新市长——城市信息请求
    @RequestMapping("updatemayorcitycommit")
    public String updatemayorcitycommit(int cid,String mname, String mgender, int mage, String mtel,int carea,String cname,int cpopulation){
        Mayor mayor=new Mayor();
        mayor.setCid(cid);
        mayor.setMage(mage);
        mayor.setMname(mname);
        mayor.setMgender(mgender);
        mayor.setMtel(mtel);
        City city=new City();
        city.setCid(cid);
        city.setCarea(carea);
        city.setCpopulation(cpopulation);
        city.setCname(cname);
        mayor.setCity(city);
       cityService.updateMayor(mayor);
        return "redirect:/mayorlist";
    }

}
